let lastCopiedPassword = null;

function generatePasswords() {
    const length = parseInt(document.getElementById('length').value);
    const includeUpperCase = document.getElementById('includeUpperCase').checked;
    const includeNumbers = document.getElementById('includeNumbers').checked;
    const includeSpecialChars = document.getElementById('includeSpecialChars').checked;

    const lowerCaseChars = 'abcdefghijklmnopqrstuvwxyz';
    const upperCaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    const numberChars = '0123456789';
    const specialChars = '!#$%*()_+[]{}<>?';

    let allChars = lowerCaseChars;
    if (includeUpperCase) allChars += upperCaseChars;
    if (includeNumbers) allChars += numberChars;
    if (includeSpecialChars) allChars += specialChars;

    const passwords = [];
    for (let i = 0; i < 21; i++) {
        passwords.push(generatePassword(length, includeUpperCase, includeNumbers, includeSpecialChars, lowerCaseChars, upperCaseChars, numberChars, specialChars, allChars));
    }

    displayPasswords(passwords);
}

function generatePassword(length, includeUpperCase, includeNumbers, includeSpecialChars, lowerCaseChars, upperCaseChars, numberChars, specialChars, allChars) {
    let password = '';
    const requiredChars = [];

    if (includeUpperCase) requiredChars.push(getRandomChar(upperCaseChars));
    if (includeNumbers) requiredChars.push(getRandomChar(numberChars));
    if (includeSpecialChars) requiredChars.push(getRandomChar(specialChars));

    for (let i = 0; i < length - requiredChars.length; i++) {
        password += getRandomChar(allChars);
    }

    // Shuffle required characters into the password
    password += requiredChars.join('');
    return shuffleString(password);
}

function getRandomChar(chars) {
    const randomIndex = Math.floor(Math.random() * chars.length);
    return chars[randomIndex];
}

function shuffleString(str) {
    const array = str.split('');
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array.join('');
}

function displayPasswords(passwords) {
    const passwordsOutput = document.getElementById('passwordsOutput');
    passwordsOutput.innerHTML = '';

    passwords.forEach(password => {
        const col = document.createElement('div');
        col.className = 'col-md-4';
        const passwordDiv = document.createElement('div');
        passwordDiv.className = 'password';
        passwordDiv.innerText = password;
        passwordDiv.onclick = () => copyToClipboard(passwordDiv);
        col.appendChild(passwordDiv);
        passwordsOutput.appendChild(col);
    });
}

function copyToClipboard(passwordDiv) {
    if (lastCopiedPassword) {
        lastCopiedPassword.classList.remove('copied');
    }

    const textarea = document.createElement('textarea');
    textarea.value = passwordDiv.innerText;
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand('copy');
    document.body.removeChild(textarea);

    passwordDiv.classList.add('copied');
    lastCopiedPassword = passwordDiv;

    // setTimeout(() => {
    //     passwordDiv.classList.remove('copied');
    // }, 2000);
}

document.addEventListener('DOMContentLoaded', generatePasswords);
